package com.rc.web.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import com.mybatisflex.core.paginate.Page;
import com.rc.pojo.dto.plant.PagePlantDTO;
import com.rc.pojo.dto.plant.PlantAddDTO;
import com.rc.pojo.dto.plant.PlantUpdateDTO;
import com.rc.pojo.vo.plant.PlantPageVO;
import com.rc.pojo.vo.plant.PlantVO;
import com.rc.web.service.PlantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *  控制层。
 *
 * @author sym
 * @since 2024-05-26
 */
@RestController
@RequestMapping("/plant")
@Api(tags = "植物")
@RequiredArgsConstructor
public class PlantController {
    private final PlantService plantService;
    @PostMapping
    @ApiOperation("添加植物")
    public void add(@RequestBody @Validated PlantAddDTO dto){
        plantService.add(dto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除植物")
    public void deleteOneById(@PathVariable @NotNull Long id){plantService.deleteOneById(id);}

    @PutMapping
    @ApiOperation("修改植物")
    public void updateOneById(@RequestBody @Validated PlantUpdateDTO dto){
        plantService.updateOneById(dto);
    }

    @GetMapping("/list")
    @ApiOperation("后台获取所有植物列表")
    @SaIgnore
    public List<PlantVO> getList() {
        return plantService.getList();
    }

    @GetMapping("/page")
    @ApiOperation("分页查看所有植物")
    public Page<PlantPageVO> getPage(PagePlantDTO dto) {
        return plantService.getPage(dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("查看植物详情")
    public PlantVO getOneById(@PathVariable Long id) {
        return plantService.getOneById(id);
    }
}
